/*
 * @Author: @yzcheng
 * @Date: 2021-04-17 18:14:09
 * @Version: 1.0
 * @LastEditors: @yzcheng
 * @Description:
 * @LastEditTime: 2021-06-01 17:20:55
 */
import { delRoadData, requestRoadList, changeRoadData } from '@/api/road'
import { ProvinceQuery } from '@/api/line'
// import { ObjectType } from '../../../typings/interfaces'
// import { MATERIALTYPE } from '@/views/line-info/roadSurface/constants.tsx'
import { requestDictCodeList } from '@/api/roadSurface'
import { ElMessage } from 'element-plus'

const road: any = {
  namespace: true,
  state: {
    roadList: null,
    dictCodeList: null,
    Provinces: [],
  },
  mutations: {
    saveDictCodeList(state: any, payload: any) {
      state.dictCodeList = payload
    },
    saveProvinces(state: any, payload: any) {
      state.Provinces = payload
    },
  },
  actions: {
    // 请求路段分页
    async getRoadList(state: any, { params, data }: never) {
      const rst = await requestRoadList(params, data)
      return rst.data.data
    },
    // 新增路段信息
    async changeRoadData(state: any, params: never) {
      await changeRoadData(params)
      ElMessage.success('添加成功')
    },
    /**
     * 获取省市区
     *
     * @param {*} state
     * @param {never} params
     */
    async getRoadProvinces({ commit }: any, params: never) {
      const rst = await ProvinceQuery(params)
      const data = rst.data.data.children.map((item) =>
        Object.assign(item, {
          value: item.regionCode,
          label: item.regionName,
          leaf: item.regionLevel > 2,
        })
      )
      commit('saveProvinces', data)
      return data
    },
    // 获取字典信息
    async getRoadDictCodeList({ commit }: any, payload: any) {
      const rst = await requestDictCodeList(payload)
      const data = rst.data.data
      const obj = {}
      for (const key in data) {
        obj[key] = data[key].map((item) => {
          item.value = item.id
          item.label = item.itemText
          return item
        })
      }
      commit('saveDictCodeList', obj)
    },
    async delRoadData(state: any, params: never) {
      await delRoadData(params)
    },
  },
}

export default road
